package thread;

import java.util.Scanner;

// 线程的 中断/终止 手动
public class demo8 {

    // 使用自定义的变量来作为标志位.
    private static boolean running = true;

    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            // 内部类成员 访问 外部类成员变量
           while (running) {
               System.out.println("hello thread");
               try {
                   Thread.sleep(2000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
            System.out.println("t 线程结束");
        });

        t.start();

        Scanner scanner = new Scanner(System.in);
        System.out.println("输入整数 0, 让 t 线程终止");
        int n = scanner.nextInt();
        if (n == 0) {
            running = false;
        }
    }
}
